<!-- src/page/hrpages/hr_register.vue -->
<template>
  <div class="register-container" style="max-width: 500px; margin: 100px auto; padding: 30px; border: 1px solid #eaeaea; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1);">
    <h2 style="text-align: center; margin-bottom: 20px;">HR 注册</h2>
    <el-form :model="form" :rules="rules" ref="registerForm" label-width="90px">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="用于登录"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="设置登录密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" type="email"></el-input>
      </el-form-item>
      <el-form-item label="公司ID" prop="companyId">
        <el-input-number v-model="form.companyId" :min="1" style="width: 100%;"></el-input-number>
      </el-form-item>
      <el-form-item label="岗位ID" prop="jobId">
        <el-input-number v-model="form.jobId" :min="1" style="width: 100%;"></el-input-number>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitRegister" style="width: 100%;">注册</el-button>
      </el-form-item>
      <div style="text-align: center;">
        <el-link type="primary" @click="$router.push('/hr_login')">已有账号？去登录</el-link>
      </div>
    </el-form>
  </div>
</template>

<script>
import request from '@/utils/request';

export default {
  data() {
    return {
      form: {
        username: '',
        password: '',
        name: '',
        email: '',
        companyId: 1,
        jobId: 1
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '邮箱格式不正确', trigger: 'blur' }
        ],
        companyId: [{ required: true, message: '请选择公司ID', trigger: 'change' }],
        jobId: [{ required: true, message: '请选择岗位ID', trigger: 'change' }]
      }
    };
  },
  methods: {
    submitRegister() {
      this.$refs.registerForm.validate(valid => {
        if (!valid) return;

        request.post('/HR_Register', this.form)
          .then(res => {
            if (res.data.code === 1) {
              this.$message.success('注册成功，即将跳转登录');
              setTimeout(() => {
                this.$router.push('/hr_login');
              }, 800);
            } else {
              this.$message.error(res.data.msg || '注册失败');
            }
          })
          .catch(() => {
            this.$message.error('请求失败，请检查网络');
          });
      });
    }
  }
};
</script>

<style scoped>
.register-container {
  background: #fff;
}
</style>